/** 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.tompai.management.busmanager;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.tompai.common.model.VipCode;
import com.tompai.common.util.Tools;
import com.tompai.foreign.service.VipCodeService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 支付相关管理
 *
 * @author liinux@qq.com
 * @date 2019/4/6 9:10
 */
@Controller
@Api(tags = "VIP管理{支付管理:/payManager}")
@RequestMapping("/pay/manager")
public class PayManagerController {

	@Resource
	private VipCodeService vipCodeService;

	/**
	 * Vip管理
	 */
	@GetMapping(value = { "/vipCode.html", "" })
	@ApiOperation(value = "Vip管理页面")
	public String vipCode(ModelMap map) {
		List<VipCode> list = vipCodeService.listIsUse();
		map.addAttribute("vipCodes", list);
		return "manager/vip-manager";
	}

	/**
	 * 创建VIP_CODE
	 *
	 * @param num
	 * @return
	 */
	@PostMapping(value = "/createVipCode.html")
	@ResponseBody
	@ApiOperation(value = "创建VIP CODE")
	public String createVipCode(String num) {
		JSONObject jsonObject = new JSONObject();
		/** 生成VIP号 */
		if (StringUtils.isNotBlank(num)) {
			int n = Integer.parseInt(num);
			VipCode vipCode;
			List<VipCode> vipCodes = new ArrayList<>();
			for (int i = 0; i < n; i++) {
				vipCode = new VipCode();
				vipCode.setCode(Tools.UUID());
				vipCode.setIsUse(1);
				vipCode.setId(Tools.UUID());
				vipCode.setCreateTime(new Date());
				vipCode.setExpireTime(new Date());
				vipCodes.add(vipCode);
			}
			int rtn = vipCodeService.saveAll(vipCodes);
			if (rtn != 0) {
				jsonObject.put("code", "1");
				jsonObject.put("data", vipCodes);
			}
		} else {
			jsonObject.put("code", "0");
		}
		return jsonObject.toString();
	}
}
